package com.base.hong.widget

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.view.View


/**
 * @ClassName ZZTCustomView
 * @Description TODO
 * @Author Hong
 * @Date 2022/10/13 15:27
 * @Version 1.0
 */
open class ZZTCustomView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {


    val mPaint1 by lazy {
        Paint().also {
            it.isAntiAlias = true
            it.color = Color.BLACK
            it.strokeWidth = 1f
            it.style = Paint.Style.STROKE
        }
    }
    val mPaint2 by lazy {
        Paint().also {
            it.isAntiAlias = true
            it.color = Color.RED
            it.strokeWidth = 5f
            it.style = Paint.Style.STROKE
        }
    }
    val mPaint3 by lazy {
        Paint().also {
            it.isAntiAlias = true
            it.color = Color.RED
            it.textSize = 30f
        }
    }

    init {

    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val width = MeasureSpec.getSize(widthMeasureSpec)
        val height = MeasureSpec.getSize(heightMeasureSpec)
        val mMin = width.coerceAtMost(height)
        setMeasuredDimension(mMin, mMin)
    }


    override fun onDraw(canvas: Canvas?) {
        onDrawGrid(canvas)
        onDrawTest(canvas)

    }

    private fun onDrawTest(canvas: Canvas?) {
        val path = Path()
        path.moveTo(paddingNum, width - paddingNum)
        path.lineTo(paddingNum + (spac * 1), width - paddingNum - (spac * 1))
        path.lineTo(paddingNum + (spac * 2), width - paddingNum - (spac * 2))
        canvas?.drawPath(path, mPaint2)

    }

    var paddingNum = 50f
    var spac = 0f

    //画网格
    private fun onDrawGrid(canvas: Canvas?) {

        spac = (width - 100) / 5f
        for (i in 0 until 6) {
            var y = i * spac
            canvas?.drawLine(
                paddingNum,
                y + paddingNum,
                width.toFloat() - paddingNum,
                y + paddingNum,
                mPaint1

            )
            canvas?.drawText((5f - i.toFloat()).toString(), 0f, y + paddingNum, mPaint3)
        }
        for (i in 0 until 6) {
            var x = i * spac
            canvas?.drawLine(

                x + paddingNum,
                paddingNum,
                x + paddingNum,
                height.toFloat() - paddingNum,
                mPaint1
            )
        }


    }

}